<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Extensions\CaptchaButton;
use App\Admin\Repositories\WxCaptchaImg;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxCaptchaImgController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxCaptchaImg(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->quickSearch(['id', 'md5','code'])->placeholder('搜索ID，md5，匹配码...')->width(50);
            $grid->simplePaginate();
            $grid->column('id')->sortable();
            $grid->column('md5');
            $grid->column('img')->image();
            $grid->column('status')->switch();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->tools([
                new CaptchaButton(),
            ]);

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxCaptchaImg(), function (Show $show) {
            $show->field('id');
            $show->field('md5');
            $show->field('img');
            $show->field('status');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxCaptchaImg(), function (Form $form) {
            $form->display('id');
//            $form->text('md5');
            $form->image('img')->url('files/uploads')->uniqueName()->autoUpload()->help('需要里面的人物是正的，其他没有要求，如果人物是斜着的话，会影响用户的判断， <a href="https://img.mini.minisns.cn/2024/04/05/0/05cccdafb5a3aa1bb3bae084541ab0dd.jpg" target="_blank">点击下载默认图</a>')->required();
            $form->switch('status')->default(1);

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
